<?php

namespace app\reserve\controller;

use think\Controller;
use think\Request;
use app\reserve\model\AdminGroup as AdminGroupModel;

class Admingroup extends Controller
{
    protected $model = '';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        return $this->model = new AdminGroupModel();
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenhong~
     * 权限组列表
     */
    public function selectAuthGroupList()
    {
        $where = array_filter([
            'group_name' => input('keyword') ? ['like', '%' . input('keyword') . '%'] : '',
            'status' => ['neq', 9]
        ], function ($data) {
            if ($data) {
                return true;
            } else {
                return false;
            }
        });
        $page = input('page') ?: 1;
        $bool = input('type') == 'all' ? true : false;
        $result = $this->model->getAuthGroupList($where, $page, $bool);
        return apiResponse(200, '查询成功', $result);
    }

    /**
     * @param Request $request
     * @return mixed
     * @author zhenhong~
     * 新增权限分组
     */
    public function addMyAuthGroup(Request $request)
    {
        $post = $request->post();
        $check = $this->validate($post, 'AdminGroup');
        if ($check !== true) {
            return apiResponse(201, $check, '');
        }
        $num = $this->model->addAuthGroup($post);
        if ($num) {
            return apiResponse(200, '新增分组成功', '');
        } else {
            return apiResponse(201, '新增失败', '');
        }
    }


    /**
     * @param Request $request
     * @return mixed
     * @author zhenhong~
     * 修改权限
     */
    public function editMyAuthGroup(Request $request)
    {
        $post = $request->post();
        $check = $this->validate($post, 'AdminGroup');
        if ($check !== true) {
            return apiResponse(201, $check, '');
        }
        $num = $this->model->updateAuthGroup($post);
        if ($num) {
            return apiResponse(200, '修改分组成功', '');
        } else {
            return apiResponse(201, '修改成功', '');
        }
    }

    /**
     * @return mixed
     * @throws \think\exception\DbException
     * @author zhenhong~
     * 查看详情
     */
    public function detailsAuthGroup()
    {
        $id = Request::instance()->post('group_id');
        $result = $this->model->getDetailsById($id);
        return apiResponse(200, '查询成功', $result);
    }

    /**
     * @return mixed
     * @author zhenhong~
     * 禁用-启用权限组
     */
    public function handleAuthGroup()
    {
        $post = Request::instance()->post();
        $arr = $this->model->setAuthStatus($post);
        if ($arr['result']) {
            return apiResponse(200, $arr['msg'], '');
        } else {
            return apiResponse(201, '操作失败', '');
        }
    }
}